package com.wcity.manage.web;

import com.wcity.manage.common.bean.RetBean;
import com.wcity.manage.common.context.Constants;
import com.wcity.manage.common.context.SessionKey;
import com.wcity.manage.common.enums.Code;
import com.wcity.manage.common.exception.CustomException;
import com.wcity.manage.common.utils.JSONUtil;
import com.wcity.manage.common.utils.StringUtil;
import com.wcity.manage.dao.model.User;
import com.wcity.manage.service.AuthService;
import com.wcity.manage.service.CommentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by jn on 2016/12/9.
 * 公共功能
 */
@Controller
public class CommonAct {

    /**
     * 公用图片上传
     * @param file
     * @return
     */
    @RequestMapping("image_upload")
    @ResponseBody
    public Map<String,Object> imageUpload(MultipartFile file, HttpServletRequest request, String registerId, String token){

        User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );

        String errorMsg;

        if ( user != null ) {
            // 后台图片上传
            errorMsg = commentService.uploadPic( file );
        } else {
            // 前端验证
            String validate = authService.checkToken( token, registerId );

            try {
                if ( file != null && file.getSize() > 0 ) {
                    errorMsg = StringUtil.isBlank( validate ) ? commentService.uploadPic( file ) : validate;
                } else {
                    String base64 = request.getParameter( "imgStr" );

                    errorMsg = commentService.uploadImageBase64( base64 );
                }
            } catch (Exception e) {
                errorMsg = "上传失败";
                e.printStackTrace();
            }
        }

        if ( StringUtil.isNumeric( errorMsg ) ) throw new CustomException( errorMsg, Code.ERROR );

        Map<String,Object> map = JSONUtil.getSuccessJson();
        map.put( "url", errorMsg );
        return map;
    }

    /**
     * 图片查看公用ctrl
     * @return
     */
    @RequestMapping("image_list")
    public ModelAndView imageList(String url){

        ModelAndView mav = new ModelAndView("imagelist");
        mav.addObject("images", commentService.imageList( url ) );
        return mav;
    }

    /**
     * 获取验证码
     * @param phone
     * @param type
     * @return
     */
    @RequestMapping("get_verify_code")
    @ResponseBody
    public RetBean getVerifyCode(String phone, String type){

        if ( !StringUtil.isNumeric( phone, 11 ) ) throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );
        if ( !( "1".equals( type ) ) )
            throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );

        String errorMsg = commentService.getCode( phone, Integer.parseInt( type ) );

        if ( !StringUtil.isBlank( errorMsg ) ) throw new CustomException( errorMsg, Code.ERROR );

        return new RetBean( Code.SUCCESS, "获取成功" );
    }

    @Resource private CommentService commentService;
    @Resource private AuthService authService;

}
